{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 02\n",
    "\n",
    "# 矩阵乘积\n",
    "Book_4《矩阵力量》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98f9659f-671c-40ba-bf01-26b02f299f65",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "此代码定义了两个 $2 \\times 2$ 的矩阵 $A$ 和 $B$，并计算它们的矩阵乘积。矩阵 $A$ 和 $B$ 的定义分别为：\n",
    "\n",
    "$$\n",
    "A = \\begin{bmatrix} 2 & 3 \\\\ 3 & 4 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 3 & 4 \\\\ 5 & 6 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "矩阵乘积 $A @ B$ 的计算公式是：\n",
    "\n",
    "$$\n",
    "A @ B = \\begin{bmatrix} 2 \\cdot 3 + 3 \\cdot 5 & 2 \\cdot 4 + 3 \\cdot 6 \\\\ 3 \\cdot 3 + 4 \\cdot 5 & 3 \\cdot 4 + 4 \\cdot 6 \\end{bmatrix} = \\begin{bmatrix} 21 & 26 \\\\ 29 & 38 \\end{bmatrix}\n",
    "$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7d099d5-b19e-4dc1-bcbc-c6f27ff10d63",
   "metadata": {},
   "source": [
    "## 导入所需库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fe40fe29-6b65-47eb-a82d-c9739d4f17b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入NumPy库，用于数值计算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "972ceb2f-dfc9-406b-aa4d-759d8a3c5ceb",
   "metadata": {},
   "source": [
    "## 定义两个矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "022dfa07-a9e4-4ba6-9456-f7dd6b861bcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[2, 3],  # 定义矩阵A\n",
    "              [3, 4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eba046cd-9051-403b-86a4-5586226e9c86",
   "metadata": {},
   "outputs": [],
   "source": [
    "B = np.array([[3, 4],  # 定义矩阵B\n",
    "              [5, 6]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97150b17-9793-47aa-b9c1-aa61baf34bde",
   "metadata": {},
   "source": [
    "## 计算矩阵点积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dec0b7d8-6d24-4d13-8bf2-0786a4d1e8e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[21, 26],\n",
       "       [29, 36]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_dot_B = np.dot(A, B)  # 使用np.dot计算A和B的矩阵乘积\n",
    "A_dot_B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[21, 26],\n",
       "       [29, 36]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可以直接用A @ B作为矩阵乘法的简写\n",
    "A @ B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
